//
//  CZStatusPictureView.swift
//  sinaWeiboswift
//
//  Created by 段文杰 on 16/4/15.
//  Copyright © 2016年 段文杰. All rights reserved.
//

import UIKit
import SnapKit
/// collectionViewcell的间距
private let HMStatusPictureViewCellMargin: CGFloat = 4

/// cell的最大列数为3列,列数使用整数
private let HMStatusPictureViewMaxColumn = 3

let CZstatusPictureItemWH = (UIScreen.mainScreen().bounds.width - 2 * HMStatusCellMargin - (CGFloat(HMStatusPictureViewMaxColumn) - 1) * HMStatusPictureViewCellMargin) / CGFloat(HMStatusPictureViewMaxColumn)

//let WH = (UIScreen.mainScreen().bounds.width - 2 *HMStatusCellMargin - (CGFloat(HMStatusPictureViewMaxColumn)-1) * HMStatusPictureViewCellMargin) / CGFloat (HMStatusPictureViewMaxColumn)

class CZStatusPictureView: UICollectionView {
    
    /// 配图NSURL数组
    var pictureURLs: [NSURL]?{
        didSet{
            let size = calcViewSize()
            
            // 自己来约束大小,当cell给pictureURLs之后计算出宽高,然后重行修改约束
//            self.snp_updateConstraints { (make) -> Void in
//                make.size.equalTo(size)
//                
//            }
            self.snp_makeConstraints { (make) in
            make.size.equalTo(size)

            }
//            HMPrint("配图大小: \(size)")
           
            self.reloadData()//刷新数据
        
        
        }
    
    
    }
    
    func calcViewSize()->CGSize {
      //判断有没有图片

        flowLayout.minimumInteritemSpacing = 0
        flowLayout.minimumLineSpacing = 0
        
        flowLayout.itemSize = CGSize(width: CZstatusPictureItemWH, height: CZstatusPictureItemWH)
       
    
        let count = pictureURLs?.count  ?? 0
        
        
        
        if count == 0 {
            HMPrint("没有图片")
            return CGSizeZero
        }
        
//        //一张图片
//        if count == 1 {
//            self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
//            return CGSizeMake(180, 90)
//        }
        // 1.张图片
        if count == 1 {
            // TODO: 返回图片本来的大小
            let size = CGSize(width: 180, height: 90)
            flowLayout.itemSize = size
            return size
        }

        
        var column = HMStatusPictureViewMaxColumn
        flowLayout.minimumInteritemSpacing = HMStatusPictureViewCellMargin
        flowLayout.minimumLineSpacing = HMStatusPictureViewCellMargin
        //两张图片
        if count == 2 || count == 4 {
            column = 2
        }
        //不是两张以外的多张图片
        let row = (count + colum-1) / column
        
        let width = CGFloat(column)*CZstatusPictureItemWH + (CGFloat(column)-1)*HMStatusPictureViewCellMargin
        let height = CGFloat(row)*CZstatusPictureItemWH + (CGFloat(row)-1)*HMStatusPictureViewCellMargin
        self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        return CGSizeMake(width, height)
        
        
        
    }
    
    
    var  flowLayout = UICollectionViewFlowLayout()
    

    
    
    init(){
    super.init(frame: CGRectZero, collectionViewLayout: flowLayout)
    
        self.dataSource = self
        self.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        
        
        //注册
        self.registerClass(CZStatusPictureCell.self, forCellWithReuseIdentifier: "cell")
//        self.registerClass(UICollectionViewCell.self, forCellWithReuseIdentifier: "cell")
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    
}
extension CZStatusPictureView: UICollectionViewDataSource{
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        HMPrint(pictureURLs)
         return pictureURLs?.count ?? 0
//        return 20
    }
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath) as! CZStatusPictureCell
        
        let picture = self.pictureURLs![indexPath.item]
    
        cell.imageURL = picture
        return cell
//        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("cell", forIndexPath: indexPath)
//        cell.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
//        return cell
        
        
    }
    
}




class CZStatusPictureCell : UICollectionViewCell {


    var imageURL : NSURL? {
    
        didSet{
        
            if let url = imageURL {
                bkgImageView.sd_setImageWithURL(url)
            }
        
        }
    
    
    }
    override init(frame: CGRect) {
        super.init(frame: frame)
        //添加子控件
        self.contentView.addSubview(bkgImageView)
        
        bkgImageView.backgroundColor = UIColor.init(red: ( CGFloat(arc4random_uniform(256)) / 255.0), green: ( CGFloat(arc4random_uniform(256)) / 255.0), blue: ( CGFloat(arc4random_uniform(256)) / 255.0), alpha: 1.0)
        
        
        bkgImageView.contentMode = UIViewContentMode.ScaleToFill
        bkgImageView.clipsToBounds = true
        
        //添加约束
        
        
        bkgImageView.snp_makeConstraints { (make) in
            make.edges.equalTo(contentView)
        }
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }




    private lazy var bkgImageView : UIImageView = UIImageView()


}


